package com.ruyuan.commodity.consumer.core.consumer.goodssearch;

import com.ruyuan.commodity.api.SearchKeywordClient;
import com.ruyuan.commodity.consumer.core.event.SearchKeywordEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 搜索关键词记录历史
 */
@Service
public class SearchKeywordUpdateConsumer  implements SearchKeywordEvent {

    private static final Logger LOG = LoggerFactory.getLogger(SearchKeywordUpdateConsumer.class);

    @Autowired
    private SearchKeywordClient searchKeywordClient;

    @Override
    public void updateOrAdd(String keyword) {
        LOG.info("进入新增或修改关键词的逻辑... 关键词为 [{}]", keyword);
        if (!StringUtils.isEmpty(keyword)) {
            // 判断关键词是否存在
            boolean isExit = searchKeywordClient.isExist(keyword);
            if (!isExit) {
                // 新增关键词
                LOG.info("新增关键词逻辑...");
                this.searchKeywordClient.add(keyword);
            } else {
                // 更新关键词
                LOG.info("更新关键词逻辑...");
                this.searchKeywordClient.update(keyword);
            }
        }
    }
}
